import 'package:flutter/material.dart';
import 'pages/accounting_page.dart';
import 'pages/statistics_page.dart';
import 'pages/settings_page.dart';
import 'pages/add_transaction_page.dart';
import 'services/theme_service.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 加载保存的主题设置
  final themeMode = await ThemeService.instance.getThemeMode();
  final primaryColor = await ThemeService.instance.getPrimaryColor();
  
  runApp(MyApp(
    initialThemeMode: themeMode,
    initialPrimaryColor: primaryColor,
  ));
}

class MyApp extends StatefulWidget {
  final ThemeMode initialThemeMode;
  final Color initialPrimaryColor;

  const MyApp({
    super.key,
    required this.initialThemeMode,
    required this.initialPrimaryColor,
  });

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late ThemeMode _themeMode;
  late Color _primaryColor;

  @override
  void initState() {
    super.initState();
    _themeMode = widget.initialThemeMode;
    _primaryColor = widget.initialPrimaryColor;
  }

  void _handleThemeModeChanged(ThemeMode mode) async {
    setState(() {
      _themeMode = mode;
    });
    await ThemeService.instance.setThemeMode(mode);
  }

  void _handlePrimaryColorChanged(Color color) async {
    setState(() {
      _primaryColor = color;
    });
    await ThemeService.instance.setPrimaryColor(color);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '简约记账',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: _primaryColor,
          brightness: Brightness.light,
        ),
        useMaterial3: true,
      ),
      darkTheme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: _primaryColor,
          brightness: Brightness.dark,
        ),
        useMaterial3: true,
      ),
      themeMode: _themeMode,
      home: HomePage(
        themeMode: _themeMode,
        onThemeModeChanged: _handleThemeModeChanged,
        primaryColor: _primaryColor,
        onPrimaryColorChanged: _handlePrimaryColorChanged,
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  final ThemeMode themeMode;
  final ValueChanged<ThemeMode> onThemeModeChanged;
  final Color primaryColor;
  final ValueChanged<Color> onPrimaryColorChanged;

  const HomePage({
    super.key,
    required this.themeMode,
    required this.onThemeModeChanged,
    required this.primaryColor,
    required this.onPrimaryColorChanged,
  });

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;
  
  final List<Widget> _pages = [
    const AccountingPage(),
    const StatisticsPage(),
    const SettingsPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_selectedIndex],
      bottomNavigationBar: NavigationBar(
        selectedIndex: _selectedIndex,
        onDestinationSelected: (index) {
          setState(() {
            _selectedIndex = index;
          });
        },
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.account_balance_wallet),
            label: '记账',
          ),
          NavigationDestination(
            icon: Icon(Icons.insert_chart),
            label: '统计',
          ),
          NavigationDestination(
            icon: Icon(Icons.settings),
            label: '设置',
          ),
        ],
      ),
      floatingActionButton: _selectedIndex == 0
          ? FloatingActionButton(
              onPressed: () async {
                final result = await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const AddTransactionPage(),
                  ),
                );
                if (result == true && mounted) {
                  setState(() {
                    _pages[0] = const AccountingPage();
                  });
                }
              },
              child: const Icon(Icons.add),
            )
          : null,
    );
  }
}

